﻿using Bangle.BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Sale_SaleList : BasePage
{
    private static string FModuleCode = "005001";

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        setUserControlStatus();
    }
    #endregion

    #region 检验用户权限
    private void setUserControlStatus()
    {
        //检验用户权限
        base.CheckUserPermission(FModuleCode);
    }
    #endregion

    #region Ajex获取列表数据
    [WebMethod]
    public static AjexResponseModel GetGridDataHtml(int pageSize, int pageIndex, string OrderField, string QueryString, int SaleTimeDay)
    {
        AjexResponseModel response = new AjexResponseModel();

        #region 用户权限
        if (!CheckUserPermissionOperation2(FModuleCode, "View"))
        {
            response.msg = "没有操作权限！请联系管理员";
            return response;
        }
        #endregion

        #region 组装查询条件
        string sWhere = " 1=1 ";
        //客户类型
        StringBuilder sQuerySqlString = new StringBuilder();
        string[] Fields = { "SaleCode", "SaleTime", "CustomerName", "Salesman", "ExchangeRate", "Freight", "PayFreight", "Payment" };
        QueryString = QueryString.Trim();
        if (QueryString.Length > 0)
        {
            string[] Values = QueryString.Split(' ');
            foreach (string vItem in Values)
            {
                if (vItem != "")
                {
                    foreach (string fItem in Fields)
                    {
                        if (sQuerySqlString.Length > 1)
                        {
                            sQuerySqlString.Append(" or ");
                        }
                        sQuerySqlString.AppendFormat("{0} like '%{1}%'", fItem, vItem);
                    }
                }
            }
            sWhere += " and (" + sQuerySqlString.ToString() + ")";
        }

        //销售时间
        if (SaleTimeDay > 0)
        {
            sWhere += string.Format(" and datediff(DAY,SaleTime,getdate())<{0}", SaleTimeDay);
        }
        #endregion

        #region 排序
        if (OrderField.Trim().Length > 0)
        {
            OrderField = OrderField.Trim() + ",";
        }
        #endregion

        #region 获取数据
        DataTable dt = null;
        int pCount = 0;
        try
        {
            UtilityBLL ubll = new UtilityBLL();
            dt = ubll.GetListByPage("View_T_Sale", "*", OrderField + "CreateTime", sWhere, pageSize, pageIndex, 1, out pCount);

            #region 生成HTML
            StringBuilder sHtmlString = new StringBuilder();

            string sHtmlModel = @"
                <tr {0}>
                    <td>{1}</td>
                    <td>{2}</td>
                    <td>{3}</td>
                    <td>{4}</td>
                    <td style='text-align:right;'>{5}</td>
                    <td style='text-align:right;'>{6}</td>
                    <td style='text-align:right;'>{7}</td>
                    <td>{8}</td>
                    <td>{9}</td>
                    <td>{10}</td>
                    <td>{11}</td>
                </tr>
            ";

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string sControl = "";

                #region 操作
                string sSaleId = dt.Rows[i]["SaleId"].ToString();
                if (CheckUserPermissionOperation2(FModuleCode, "View"))
                {
                    sControl += "&nbsp;&nbsp;<a href='javascript:void(0)' onclick=\"showWindow('销售单查看','SaleEdit.aspx?Action=Detail&SaleId=" + sSaleId + "')\" >查看</a>";
                }
                if (CheckUserPermissionOperation2(FModuleCode, "Edit"))
                {
                    sControl += "&nbsp;&nbsp;<a href='javascript:void(0)' onclick=\"showWindow('销售单编辑','SaleEdit.aspx?Action=Modify&SaleId=" + sSaleId + "')\" >编辑</a>";
                }
                if (CheckUserPermissionOperation2(FModuleCode, "Del"))
                {
                    sControl += "&nbsp;&nbsp;<a href='javascript:void(0)' onclick=\"delGridData('SaleList.aspx/DelGridData','" + sSaleId + "')\" >删除</a>";
                }
                #endregion

                sHtmlString.Append(string.Format(sHtmlModel
                    , (i % 2 == 0 ? "" : "class='ht-grid-even'")
                    , dt.Rows[i]["SaleCode"].ToString()
                    , dt.Rows[i]["SaleTime"].ToString()
                    , dt.Rows[i]["CustomerName"].ToString()
                    , dt.Rows[i]["Salesman"].ToString()
                    , Convert.ToDecimal(dt.Rows[i]["ExchangeRate"]).ToString("n2")
                    , Convert.ToDecimal(dt.Rows[i]["Freight"]).ToString("n2")
                    , Convert.ToDecimal(dt.Rows[i]["PayFreight"]).ToString("n2")
                    , dt.Rows[i]["Payment"].ToString()
                    , dt.Rows[i]["StateName"].ToString()
                    , dt.Rows[i]["Description"].ToString()
                    , sControl
                    ));
            }
            #endregion

            response.gridHtml = sHtmlString.ToString();
            response.recordCound = pCount;
            response.pageIndex = pageIndex;
            response.pageSize = pageSize;
        }
        catch (Exception ex)
        {
            response.msg = ex.ToString();
        }
        #endregion

        return response;
    }

    [WebMethod]
    public static string DelGridData(string Id)
    {
        T_SaleBLL bll = new T_SaleBLL();
        string sMsg = "";

        try
        {
            bll.Delete(Guid.Parse(Id));
        }
        catch (Exception ex)
        {
            sMsg = "操作失败！";
        }
        return sMsg;
    }
    #endregion
}